#include<bits/stdc++.h>
#define rg register
#define file(x)freopen(x".in","r",stdin);freopen(x".out","w",stdout)

using namespace std;

namespace Euler{
	const int maxV=1e6+10;
	int prime[maxV],top;
	bool vis[maxV];
	bool Key[maxV];
	inline void init(const int Lim=1e6)
	{
		for(rg int i=2;i<=Lim;i+=1)
		{
			if(!vis[i])prime[++top]=i;
			for(rg int j=1;j<=top&&1ll*prime[j]*i<=Lim;j+=1)
			{
				vis[prime[j]*i]=true;
				if(i%prime[j]==0)break;
			}
		}
		for(rg int i=2;i<=Lim;i+=1)
			if(!vis[i]&&(i<=10||i%20==3))
				Key[i]=true;
	}
}

const int maxn=8e3+10;

int n,m;
pair<int,int>id[maxn];
int Value[maxn];

inline int calc(pair<int,int>one,pair<int,int>two)
{
	return abs(one.first-two.first)+abs(one.second-two.second);
}
inline bool check(int a,int b,int c)
{
	vector<int>dis={calc(id[a],id[b]),calc(id[a],id[c]),calc(id[b],id[c])};
	sort(dis.begin(),dis.end());
	return Euler::Key[dis[1]];
}
inline void solve()
{
	scanf("%d %d",&n,&m);
	for(rg int i=1;i<=n;i+=1)
	{
		auto &[x,y]=id[i];
		scanf("%d %d",&x,&y);
	}
	for(rg int i=1;i<=n;i+=1)scanf("%d",Value+i);
	long long Ans=0;
	for(rg int i=1;i<=n;i+=1)
		for(rg int j=i+1;j<=n;j+=1)
			for(rg int k=j+1;k<=n;k+=1)
				if(check(i,j,k))
					Ans+=Value[i]+Value[j]+Value[k];
	printf("%lld\n",Ans);
}

int main()
{
	file("chess");
	Euler::init();
	int T;scanf("%d",&T);
	while(T--)solve();
	return 0;
}
